<?php
// 应用公共文件
/**
 * @param int $status 1:成功、0:失败
 * @param string $message 提示信息
 * @param array $data 数据
 * @param int $httpCode http状态码
 * @return array
 */
function show($status = 0, $message = '', $data = [], $httpCode = 200) {
    $result = [
        'status' => $status,
        'msg'    => $message,
        'data'   => $data
    ];
    return json($result, $httpCode);
}

function check_verify($code, $id = '') {
    $captcha = new \think\captcha\Captcha();
    return $captcha->check($code, $id);
}

function curl_get($url, &$httpCode = 0) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //不做证书校验，部署在Linux环境下改为true
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $file_contents = curl_exec($ch);
    $httpCode      = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $file_contents;
}

function curl_post($url, $post_data) {
    $postJsonData = json_encode($post_data);
    $ch           = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postJsonData);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $data = curl_exec($ch);
    return $data;
}

// 生成随机字符串
function getRandChar($length) {
    $str    = null;
    $strPol = "ABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789abcdefghigklmnopqrstuvwxyz";
    $max    = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];
    }
    return $str;
}

function getUploadUrl($httpUrl) {
//    $httpUrl = str_replace('/', '', $httpUrl);
    preg_match('/upload[^.]+.(jpg|png|gif$)/', $httpUrl, $matches);
    return $matches[0];
}